<?
	error_reporting(7);
	require "global.php";

     hotheader("Print Management");
     //delete all selected
    if($_GET['dele'] == 'y'){
	    unset($_SESSION['postkey']);
		unset($_SESSION['num']);
	}
	if(!empty($_POST['pd'])){
		 $diffsession  = @array_diff($_SESSION[pagekey],$_POST['pd']);
		 if(empty($_SESSION['postkey'])){$_SESSION['postkey'] = array();		
				 $_SESSION['postkey']	=    @array_unique($_POST['pd']+$_SESSION['postkey']);
         }
         else{
              $cou = count($_POST['pd']);
 		      for($i=0;$i<$cou;$i++){
			     if(@in_array($_POST['pd'][$i],$_SESSION['postkey'])){
				     //unset($_SESSION['postkey'][array_search($_POST['pd'][$i],$_SESSION['postkey'])]);
				 }else{
				     $_SESSION['postkey'][] = $_POST['pd'][$i];
				 }
			  }
			  $_SESSION['postkey']  = @array_diff($_SESSION['postkey'],$diffsession);
		 }
		 $_SESSION['num']     =    count($_SESSION['postkey']);
 	}
	cpheader();
	//print_r($_SESSION);
	//print_r($_POST);
    if(!$_SESSION[searchbase]){  
	   $_SESSION[searchbase] = "searchfabric.php?".$_SERVER[QUERY_STRING];
	}
	$url1    = $_SESSION[searchbase]."&ob1=up";
	$url2    = $_SESSION[searchbase]."&ob2=up";
	$url3    = $_SESSION[searchbase]."&ob3=up";
	$url4    = $_SESSION[searchbase]."&ob4=up";
	 $ob1   =    trim($_GET['ob1']);
	 $ob2   =    trim($_GET['ob2']);
	 $ob3   =    trim($_GET['ob3']);
	 $ob4   =    trim($_GET['ob4']);



	if ($_GET[action]=="list"||$_GET[action]=="search"||$_GET[action]=="dosearch"){


	if($_GET[action]=="list"||$_GET[action]=="search"){   
		$nav = new buildNav;


		 if($ob1!=''|| $ob2!=''|| $ob3!=''|| $ob4!=''){ $conditions .=" ORDER BY  " ; }else{$conditions = " ORDER BY a.Fabric_ID DESC";}
		 if($ob1 == 'up'){
			 $conditions .= " a.Fabric_ID ASC ";
			 $url1=  $_SESSION[searchbase]."&ob1=down";$img1='<img src="images/up.gif">';}elseif($ob1 == "down"){ $conditions .= " a.Fabric_ID DESC ";$img1='<img src="images/down.gif">';}
		 if($ob2 == 'up'){$conditions .= " a.Supplier_Art_No ASC ";
		     $url2=  $_SESSION[searchbase]."&ob2=down";$img2='<img src="images/up.gif">';}elseif($ob2 == 'down'){ $conditions .= " a.Supplier_Art_No DESC ";$img2='<img src="images/down.gif">';}
		 if($ob3 == 'up'){$conditions .= " a.Program_No ASC ";
		     $url3=  $_SESSION[searchbase]."&ob3=down"; $img3='<img src="images/up.gif">';}elseif($ob3 == 'down'){ $conditions .= " a.Program_No DESC ";$img3='<img src="images/down.gif">';}
		 if($ob4 == 'up'){$conditions .= "a.Supplier_ID ASC ";
		     $url4=  $_SESSION[searchbase]."&ob4=down";$img4='<img src="images/up.gif">';}elseif($ob4 == 'down'){ $conditions .= " a.Supplier_ID DESC ";$img4='<img src="images/down.gif">';}

		 $total = $DB->query("SELECT a.*,b.* FROM `pa_fabricinformation` as a left join `pa_hanger` as b on a.Fabric_ID = b.Fabric_ID WHERE 1 ".$conditions);

         $num = $DB->num_rows($total);
		 $nav->total_result = $num;

		if ($num<1) {
			pa_exit("<font color=red>No Fabric!</font>");
		}

		$nav->execute("SELECT a.*,b.* FROM `pa_fabricinformation` as a left join `pa_hanger` as b on a.Fabric_ID = b.Fabric_ID WHERE 1 ".$conditions);
    }
    if($_GET[action]=="dosearch"){
		    if(!$_SESSION[urlsearch]||!ereg("&ob",$_SERVER[QUERY_STRING])){  
			   $_SESSION[urlsearch] = "searchfabric.php?".$_SERVER[QUERY_STRING];
			}
				$url1    = $_SESSION[urlsearch]."&ob1=up";
				$url2    = $_SESSION[urlsearch]."&ob2=up";
				$url3    = $_SESSION[urlsearch]."&ob3=up";
				$url4    = $_SESSION[urlsearch]."&ob4=up";

		 $artNo        = trim($_GET[artNo]);
         $suppliername = trim($_GET[suppliername]);
         $materialname = trim($_GET[materialname]);
         $item         = trim($_GET[item]);
		 $composition  = trim($_GET[composition]);
		 		 		 //jason12-13
         $program_no= trim($_GET[program_no]);

         if($artNo  != '')       $condition_array[] = "  a.Fabric_ID LIKE '%".addslashes($artNo)."%' ";
	     if($suppliername != '') $condition_array[] = "  a.Supplier_Name LIKE '%".addslashes($suppliername)."%' ";
		 if($materialname != '')  $condition_array[] = " a.Marterial_Name LIKE '%".addslashes($materialname)."%' ";
		 if($item != '')          $condition_array[] = "  a.Item LIKE '%".addslashes($item)."%' ";
		 if($composition != '')  $condition_array[] = "  a.Composition LIKE '%".addslashes($composition)."%' ";
		  //jason12-13
		 if($program_no != '') $condition_array[] = "  Program_No LIKE '%".addslashes($program_no)."%' ";
		 $conditions = @implode(" AND ",$condition_array);

		 if(empty($condition_array)){
		     $conditions = 1;
		 }

		 if($ob1!=''|| $ob2!=''|| $ob3!=''|| $ob4!=''){ $conditions .=" ORDER BY  " ; }
		 if($ob1 == 'up'){$conditions .= "a.Art_No ASC ";
			 $url1=  $_SESSION[urlsearch]."&ob1=down";$img1='<img src="images/up.gif">';}elseif($ob1 == "down"){ $conditions .= " a.Art_No DESC ";$img1='<img src="images/down.gif">';}
		 if($ob2 == 'up'){$conditions .= " a.Supplier_Art_No ASC ";
		     $url2=  $_SESSION[urlsearch]."&ob2=down";$img2='<img src="images/up.gif">';}elseif($ob2 == 'down'){ $conditions .= " a.Supplier_Art_No DESC ";$img2='<img src="images/down.gif">';}
		 if($ob3 == 'up'){$conditions .= " a.Program_No ASC ";
		     $url3=  $_SESSION[urlsearch]."&ob3=down"; $img3='<img src="images/up.gif">';}elseif($ob3 == 'down'){ $conditions .= " a.Program_No DESC ";$img3='<img src="images/down.gif">';}
		 if($ob4 == 'up'){$conditions .= " a.Supplier_ID ASC ";
		     $url4=  $_SESSION[urlsearch]."&ob4=down";$img4='<img src="images/up.gif">';}elseif($ob4 == 'down'){ $conditions .= " a.Supplier_ID DESC ";$img4='<img src="images/down.gif">';}



		 $nav = new buildNav;

		 $total = $DB->query("SELECT a.*,b.* FROM `pa_fabricinformation` as a left join `pa_hanger` as b on a.Fabric_ID = b.Fabric_ID WHERE ".$conditions);

         $num = $DB->num_rows($total);
		 $nav->total_result = $num;
         
		 if(empty($total)){
			echo "<font color=red>No results!</font>";
		 }

		 $nav->execute("SELECT a.*,b.* FROM `pa_fabricinformation` as a left join `pa_hanger` as b on a.Fabric_ID = b.Fabric_ID WHERE  ".$conditions);
   
   
    }   
	

		echo ' 	
		<table border="0" width="100%" cellpadding="0" cellspacing="0" height=20>
							  <tr align="left">
							   <td width="25%" style="color:#000099;font-weight:bold">1. Search the Fabric/Hanger</td><td width="10%" >-------></td><td width="20%" >2. Review Print List</td><td width="10%" >-------></td><td width="20%" >3. Select Print Option</td>
							   <td width="20%" align="right"></td>
						   </tr>
			   </table><form method="GET" action="" name="f1">
				<table border="0" width="100%" cellpadding="4" cellspacing="1" class="tableoutline">
						  <tr align="center" class="tbhead">
					       <td nowrap ><b>Art. No.:</b></td>
							<td nowrap align=left><input  value="'.$_GET[artNo].'" name="artNo" size="20" maxlength="50" ></td>
							<td nowrap ><b>Supplier</b></td>
							<td nowrap align=left><input  value="'.$_GET[suppliername].'" name="suppliername" size="20" maxlength="50" ></td>
							<td nowrap ><b>Material</b></td>
							 <td nowrap align=left><input  value="'.$_GET[materialname].'" name="materialname" size="20" maxlength="50" ></td>
				<td nowrap rowspan="2"><input type="submit" value=" Search " width="100"><input type="hidden" name="action" value="dosearch"></td>
						   </tr>
					<tr align="center" class="tbhead">
			 <td nowrap ><b>Pgr No.</b></td>
							<td nowrap align=left><input  value="'.$_GET[program_no].'" name="program_no" size="20" maxlength="50" ></td>
					       <td nowrap ><b>Item</b></td>
							<td nowrap align=left><input  value="'.$_GET[item].'" name="item" size="20" maxlength="50" ></td>
							<td nowrap ><b>Composition</b></td>
							<td nowrap align=left><input  value="'.$_GET[composition].'" name="composition" size="20" maxlength="50" ></td>					
						   </tr>
				</table>
		  </form>';

   
    //echo $nav->pagenav();
    echo "<script type=\"text/javascript\" src=\"js/js_all.js\"></script><form action=\"\" method=\"post\" id=\"fm1\"><table border=\"0\" width=\"820\" cellpadding=\"4\" cellspacing=\"1\" class=\"tableoutline\">
	           <tr><td align=left colspan=6><INPUT type=button name=select value=\"Select All\" onclick=selcheck()> <INPUT type=submit name=select value=\"Add to Print List\" >";

	if(count($_SESSION[postkey])!=0) echo "   &nbsp;&nbsp;No. of fabric/hanger on the print list: <font color=red>".count($_SESSION[postkey])."</font>";

	echo "</td><td align=right colspan=2> <INPUT type=button  value=\"Review Print List\"  onclick='location.href=\"fabricprint.php?action=list\"'></td><tr>	
               <tr align=\"center\" class=\"tbhead\">
				 <th   width=\"30\">Print</th>
                 <th   width=\"64\"><a href=\"".$url1."\" class=\"ha\">HQ.A/N</a>".$img1."</th>
                 <th   width=\"60\">HangerID</th>
			     <th   width=\"100\">Supplier</th>
				 <th   width=\"80\">Item</th>
				 <th   width=\"72\">Material</th>
				 <th   width=\"136\">Composition</th> 
				 <th   width=\"45\">Price</th>
			   </tr>\n";
    $i=1;
	unset($_SESSION['pagekey']);
    while ($list = $DB->fetch_array($nav->sql_result)){
		  if($list[Hanger_ID]) $list[HangerID] ="H".$list[Hanger_ID];
          if($list[Price_Yard]!='0.00'){$list[Pri] = "USD$".$list[Exchange_Yard]."per yard";$list[Min]=$list[Minimum_Yard];}
		  else{$list[Pri] = "USD$".$list[Exchange_Meter]."per meter";$list[Min]=$list[Minimum_Meter];}
            $idkey = ($list[Art_No]+0).'@'.($list[Hanger_ID]+0);
			echo "<tr class=".getrowbg().">
		    <td><input type=\"checkbox\" id=\"pd".$i."\"  name=\"pd[]\" value=\"".$idkey."\" ".checkoff($idkey,$_SESSION[postkey])."></td>
                      <td align=\"left\" nowrap>".fillzero($list[Art_No])."</td>
				      <td>".$list[HangerID]."</td>
					  <td align=\"left\">".$list[Supplier_Name]."</td>
                      <td align=\"left\">".$list[Item]."</td>
					  <td align=\"left\">".$list[Marterial_Name]."</td>
					  <td align=\"left\">".$list[Composition]."</td>
					  <td align=\"left\">".$list[Pri]."</td>
                     </tr>\n";
					$i++;
					$_SESSION[pagekey][] = $idkey; 

    }
    echo "</table>
	</form>\n";
    echo $nav->pagenav();
//<INPUT type=\"button\"  value=\"Delete All Selected\"  onclick='location.href=\"".$_SESSION[searchbase]."&dele=y\"'>
}


if ($_GET[action]=="kill"){
    $cpforms->formheader(array('title'=>'Sure to delete this?'));
    $cpforms->makehidden(array('name'=>'action',
                                'value'=>'remove'));
    $cpforms->makehidden(array('name'=>'fabricID',
                                'value'=>$_GET[fabricID]));
    $cpforms->formfooter(array('confirm'=>1));
}


if( $_POST[action]=="remove"){
    $DB->query("DELETE FROM ".$db_prefix."fabricinformation WHERE fabricID='$_POST[fabricID]'");
    redirect("./fabricinformation.php?action=list","<font color=red>Cleared!</font>");
}

cpfooter();
?>